#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#define high 700
#define width 590
#pragma comment(lib,"Winmm.lib")
//函数外全局变量定义
int position_x, position_y;//飞机位置
int bullet_x, bullet_y;//子弹位置
int enemy_x, enemy_y;//敌机位置
int isExplode=0;//我机是否爆炸
int score=0;
IMAGE img_bk;//背景图片
IMAGE plane1;
IMAGE plane2;
IMAGE bullet1;//子弹图片
IMAGE bullet2;
IMAGE enemy1,enemy2;//敌机图片
IMAGE explode1,explode2;
void startup()
{
	mciSendString("open D:\\game_music.mp3 alias bkmusic",NULL,0,NULL);
	mciSendString("play bkmusic repeat",NULL,0,NULL);
	initgraph(width,high);
	loadimage(&img_bk,"D:\\background.jpg");
	loadimage(&plane1,"D:\\planeNormal_1.jpg");
	loadimage(&plane2,"D:\\planeNormal_2.jpg");
	loadimage(&bullet1,"D:\\bullet1.jpg");
	loadimage(&bullet2,"D:\\bullet2.jpg");
	loadimage(&enemy1,"D:\\enemyPlane1.jpg");
	loadimage(&enemy2,"D:\\enemyPlane2.jpg");
	loadimage(&explode1,"D:\\planeExplode_1.jpg");
	loadimage(&explode2,"D:\\planeExplode_2.jpg");
	position_x = width/2;
	position_y = high/2;
	bullet_x=-100;
	bullet_y=-1000;//起初无子弹
	enemy_x=rand()%(width-105);
	enemy_y=0;
	
	BeginBatchDraw();
}
void show()
{ 
	
	putimage(0,0,&img_bk);//显示背景
	if(isExplode==0)
	{
    
     putimage(position_x-50,position_y-70,&plane1,NOTSRCERASE);
	 putimage(position_x-50,position_y-70,&plane2,SRCINVERT);
	 putimage(bullet_x,bullet_y,&bullet1,NOTSRCERASE);
	 putimage(bullet_x,bullet_y,&bullet2,SRCINVERT);
	 putimage(enemy_x,enemy_y,&enemy1,NOTSRCERASE);
	 putimage(enemy_x,enemy_y,&enemy2,SRCINVERT);
	}
	else
	{
		putimage(position_x-50,position_y-70,&explode1,NOTSRCERASE);
	    putimage(position_x-50,position_y-70,&explode2,SRCINVERT);
	}
	outtextxy(width*0.48,high*0.85,"得分：");
	char s[5];
	sprintf(s,"%d",score);
	outtextxy(width*0.55,high*0.85,s);
	FlushBatchDraw();
	Sleep(5);
}
void updatewithinput()
{
MOUSEMSG m;//记录鼠标消息
while(MouseHit())
{
m=GetMouseMsg();
if(isExplode==0&&m.uMsg==WM_MOUSEMOVE)//我机器移动
{
position_x=m.x;
position_y=m.y;
}
else if(isExplode==0&&m.uMsg==WM_LBUTTONDOWN)//左键发射子弹
{
	mciSendString("close f_gun",NULL,0,NULL);//先把前面一次关掉
	mciSendString("open D:\\f_gun.mp3 alias f_gun",NULL,0,NULL);//发射子弹音效
	mciSendString("play f_gun",NULL,0,NULL);//仅播放一次
    bullet_x=position_x-7;
    bullet_y=position_y-90;
}
}
}
void updatewithoutinput()
{   if(bullet_y>-100) 
	bullet_y-=10;//子弹上升
if(enemy_y<high)
{
enemy_y+=1;//敌机下降
}
else
{
    enemy_y=0;
	enemy_x=rand()%(width-105);//敌机刷新
}
if(abs(enemy_y-bullet_y)<=10&&abs(enemy_x+30-bullet_x)<=50)//击中敌机
{
	mciSendString("close gotEnemy",NULL,0,NULL);//先把前面一次关掉
	mciSendString("open D:\\gotEnemy.mp3 alias gotEnemy",NULL,0,NULL);//击中音效
	mciSendString("play gotEnemy",NULL,0,NULL);//仅播放一次
    enemy_y=0;
	enemy_x=rand()%(width-105);//敌机刷新
	bullet_x=-100;
	bullet_y=-1000;
	score+=1;
}
if(abs(enemy_y+40-position_y)<=100&&abs(enemy_x+45-position_x)<=80)//敌机击中我机
{
	mciSendString("close explode",NULL,0,NULL);//先把前面一次关掉
	mciSendString("open D:\\explode.mp3 alias explode",NULL,0,NULL);//爆炸音效
	mciSendString("play explode",NULL,0,NULL);//仅播放一次
    isExplode=1;
    
}
}
void over()
{
	system("pause");
}
int main()
{
	startup();//数据初始化
	while(1)//游戏循环进行
	{   
		show();//显示画面
		updatewithinput();//与用户输入有关的更新
		updatewithoutinput();//与用户输入无关的更新
	}
	over();
	return 0;
}
